home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils misc / Boxer 0.18 / Boxer018.exe / boxer / PalmBoxer / boxer.c next >
Encoding:
C/C++ Source or Header  |  2000-07-09  |  25.7 KB  |  1,139 lines

  1. #pragma pack(2)
  2.  
  3. #include <Common.h>
  4. #include <System/SysAll.h>
  5. #include <UI/UIAll.h>
  6. #include <ExgMgr.h>
  7. #include <System/FileStream.h>
  8. #include <System/SysEvtMgr.h>
  9. #include "boxer.h"
  10.  
  11. #include <Unix/sys_types.h>
  12.  
  13. #include "stringil.h"
  14. #include "stdio2.h"
  15.  
  16. /* this is the callback stuff from Callback.h by ian in O'Reilly's book */
  17. register void *reg_a4 asm("%a4");
  18.  
  19. #define CALLBACK_PROLOGUE \
  20. void *save_a4 = reg_a4; asm("move.l %%a5,%%a4; sub.l #edata,%%a4" : :)
  21.  
  22. #define CALLBACK_EPILOGUE reg_a4 = save_a4
  23.  
  24. static int toprow = 0;
  25. static int maxrow = 0;
  26. static int selection = -1;
  27.  
  28.  
  29. static char *filelist;
  30. static char *filecard;
  31. static long int *filedisp;
  32. FileHand archfd;
  33.  
  34.  
  35. /* **************************************************************************** */
  36.  
  37. static void TableDrawCell(VoidPtr tableP, Word row, Word column,
  38.                           RectanglePtr bounds)
  39. {
  40.   char *c;
  41.   c = (char *) TblGetRowData(tableP, row);
  42.   WinDrawChars(c, strlen(c), bounds->topLeft.x, bounds->topLeft.y);
  43. }
  44.  
  45. /* **************************************************************************** */
  46.  
  47. int unzipdir(FileHand infile, char *dir, long int *loc);
  48. int untardir(FileHand fd, char *dir, long int *loc);
  49.  
  50. static void scandbs()
  51. {
  52.   LocalID lid;
  53.   UInt16 cardno, attr;
  54.   UInt32 type, crea, disp, i;
  55.   char tp[40];
  56.  
  57.   maxrow = 0;
  58.  
  59.   disp = 0;
  60.   DmSet(filelist, 0, 2, 0);
  61.  
  62.   maxrow = 0;
  63.   for (cardno = 0; cardno < MemNumCards(); cardno++) {
  64.     for (i = 0; i < DmNumDatabases(cardno); i++) {
  65.       lid = DmGetDatabase(cardno, i);
  66.       DmDatabaseInfo(cardno, lid, tp, &attr, NULL, NULL, NULL, NULL, NULL, NULL,
  67.                      NULL, &type, &crea);
  68.       if (!(attr & dmHdrAttrStream) || type != 'DATA')
  69.         continue;
  70.       if (crea != 'NZIP' && crea != 'BRWS' && crea != 'ATCH' && crea != 'BOXR')
  71.         continue;
  72.  
  73.       DmWrite(filelist, disp, tp, strlen(tp) + 1);
  74.       disp += strlen(tp) + 1;
  75.       DmWrite(filedisp, maxrow * 4, &i, 4);
  76.       DmWrite(filecard, maxrow * 2, &cardno, 2);
  77.       maxrow++;
  78.     }
  79.   }
  80.   DmSet(filelist, disp, 2, 0);
  81.   selection = -1;
  82. }
  83.  
  84. /* **************************************************************************** */
  85.  
  86. int unzip(FileHand infile, long int loc);
  87. int untar(FileHand fd, long int loc);
  88. int MakeDoc(char *name, FileHand fd, int destruct);
  89. int MakeDatabase(FileHand fd, int destruct);
  90.  
  91. void settable()
  92. {
  93.   TablePtr tptr;
  94.   Word n;
  95.   FormPtr frm;
  96.   UInt top;
  97.   Word row, numRows;
  98.   char *c;
  99.   ScrollBarPtr bar;
  100.  
  101.  
  102.   WinEraseWindow();
  103.   frm = FrmGetActiveForm();
  104.   tptr = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, maintable));
  105.   top = toprow;
  106.   numRows = TblGetNumberOfRows(tptr);
  107.   c = filelist;
  108.   n = top;
  109.   while (n-- && *c)
  110.     c += strlen(c) + 1;
  111.  
  112.   for (n = 0; n < 1; n++)
  113.     TblSetColumnUsable(tptr, n, true);
  114.  
  115.   for (row = 0; row < numRows; row++, top++) {
  116.  
  117.     if (*c) {
  118.       TblSetRowData(tptr, row, (ULong) c);
  119.       c += strlen(c) + 1;
  120.     }
  121.  
  122.     if (top < maxrow) {
  123.       TblSetRowSelectable(tptr, row, true);
  124.  
  125.       for (n = 0; n < 1; n++) {
  126.         TblSetItemStyle(tptr, row, n, customTableItem);
  127.         TblSetCustomDrawProcedure(tptr, n, TableDrawCell);
  128.       }
  129.       TblMarkRowInvalid(tptr, row);
  130.     } else
  131.       TblSetRowUsable(tptr, row, false);
  132.   }
  133.   //  selection = -1;               //Until I can get highlighting to work
  134.  
  135.   //  TblHasScrollBar (tptr, true);
  136.  
  137.   bar = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, scrl));
  138.  
  139.   if (maxrow <= numRows)
  140.     SclSetScrollBar(bar, 0, 0, 0, 10);
  141.   else
  142.     SclSetScrollBar(bar, toprow, 0, maxrow - numRows, numRows);
  143.  
  144.   FrmDrawForm(frm);
  145.  
  146.   if( selection != -1 )
  147.     TblSelectItem (tptr, selection - toprow, 0);
  148.  
  149. }
  150.  
  151. /* **************************************************************************** */
  152.  
  153. void dopage(int down)
  154. {
  155.   TablePtr tptr;
  156.   Word n;
  157.   FormPtr frm;
  158.  
  159.   frm = FrmGetActiveForm();
  160.   tptr = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, maintable));
  161.   n = TblGetNumberOfRows(tptr);
  162.   toprow += n * (((down) << 1) - 1);
  163.   if ((toprow + n) >= maxrow)
  164.     toprow = maxrow - n;
  165.   if (toprow < 0)
  166.     toprow = 0;
  167.  
  168.   FrmGotoForm(FrmGetActiveFormID());
  169. }
  170.  
  171. /* **************************************************************************** */
  172.  
  173. void doscroll()
  174. {
  175.   TablePtr tptr;
  176.   Word n;
  177.   FormPtr frm;
  178.   ScrollBarPtr bar;
  179.   Word v, mn, mx, ps;
  180.  
  181.   frm = FrmGetActiveForm();
  182.   bar = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, scrl));
  183.   SclGetScrollBar(bar, &v, &mn, &mx, &ps);
  184.   tptr = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, maintable));
  185.   n = TblGetNumberOfRows(tptr);
  186.   toprow = v;
  187.   if ((toprow + n) >= maxrow)
  188.     toprow = maxrow - n;
  189.   if (toprow < 0)
  190.     toprow = 0;
  191.  
  192.   FrmGotoForm(FrmGetActiveFormID());
  193. }
  194.  
  195. /* **************************************************************************** */
  196.  
  197. /* HANDLERS */
  198.  
  199. int basehand(EventPtr event)
  200. {
  201.   switch (event->eType) {
  202.  
  203.   case frmOpenEvent:
  204.     settable();
  205.     return 1;
  206.  
  207.   case sclExitEvent:
  208.     doscroll();
  209.     return 1;
  210.  
  211.   case keyDownEvent:
  212.     if (event->data.keyDown.chr != pageUpChr &&
  213.         event->data.keyDown.chr != pageDownChr) return 0;
  214.  
  215.     dopage(event->data.keyDown.chr == pageDownChr);
  216.     return 1;
  217.   default:
  218.     return 0;
  219.   }
  220.  
  221. }
  222.  
  223. /* **************************************************************************** */
  224.  
  225. static Boolean UnzipH(EventPtr event)
  226. {
  227.   int eid;
  228.  
  229.   if (basehand(event))
  230.     return 1;
  231.  
  232.   switch (event->eType) {
  233.  
  234.   case ctlSelectEvent:
  235.     eid = event->data.ctlEnter.controlID;
  236.     switch (eid) {
  237.     case bunzip:
  238.       if (selection == -1)
  239.         break;
  240.       WinEraseWindow();
  241.       unzip(archfd, filedisp[selection]);
  242.       FrmGotoForm(unzipform);
  243.       return 1;
  244.     case bexit:
  245.       FileClose(archfd);
  246.       scandbs();
  247.       FrmGotoForm(decomform);
  248.       return 1;
  249.     }
  250.     return 0;
  251.  
  252.   case tblSelectEvent:
  253.     selection = toprow + event->data.tblSelect.row;
  254.     return 1;
  255.     break;
  256.   default:
  257.     return 0;
  258.   }
  259.  
  260. }
  261.  
  262. /* **************************************************************************** */
  263.  
  264. static Boolean UntarH(EventPtr event)
  265. {
  266.   int eid;
  267.  
  268.   if (basehand(event))
  269.     return 1;
  270.  
  271.   switch (event->eType) {
  272.  
  273.   case ctlSelectEvent:
  274.     eid = event->data.ctlEnter.controlID;
  275.  
  276.     switch (eid) {
  277.     case buntar:
  278.       if (selection == -1)
  279.         return 1;
  280.  
  281.       WinEraseWindow();
  282.       untar(archfd, filedisp[selection]);
  283.       FrmGotoForm(untarform);
  284.       return 1;
  285.  
  286.     case bexit:
  287.       FileClose(archfd);
  288.       scandbs();
  289.       FrmGotoForm(decomform);
  290.       return 1;
  291.     }
  292.  
  293.     return 0;
  294.  
  295.   case tblSelectEvent:
  296.     selection = toprow + event->data.tblSelect.row;
  297.     return 1;
  298.   default:
  299.     return 0;
  300.   }
  301. }
  302.  
  303. /* **************************************************************************** */
  304.  
  305. static Boolean MainH(EventPtr event)
  306. {
  307.   int eid;
  308.  
  309.   if (basehand(event))
  310.     return 1;
  311.  
  312.   switch (event->eType) {
  313.  
  314.   case ctlSelectEvent:
  315.     eid = event->data.ctlEnter.controlID;
  316.  
  317.     switch (eid) {
  318.     case bfile:
  319.       FrmGotoForm(fileform);
  320.       return 1;
  321.     case bdecom:
  322.       FrmGotoForm(decomform);
  323.       return 1;
  324.     case binst:
  325.       FrmGotoForm(instform);
  326.       return 1;
  327.     default:
  328.       return 0;
  329.     }
  330.  
  331.   case tblSelectEvent:
  332.     selection = toprow + event->data.tblSelect.row;
  333.     return 1;
  334.   default:
  335.     return 0;
  336.  
  337.   }
  338. }
  339.  
  340. /* **************************************************************************** */
  341.  
  342. static Boolean DecomH(EventPtr event)
  343. {
  344.   Word n;
  345.   LocalID lid;
  346.   FileHand fd;
  347.   char name[256];
  348.   UInt32 type, crea;
  349.   int eid;
  350.  
  351.   if (basehand(event))
  352.     return 1;
  353.  
  354.   switch (event->eType) {
  355.  
  356.   case ctlSelectEvent:
  357.  
  358.     eid = event->data.ctlEnter.controlID;
  359.     if (eid == bexit) {
  360.       FrmGotoForm(mainform);
  361.       return 1;
  362.     }
  363.     if (selection == -1)
  364.       return 1;
  365.  
  366.     lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  367.     DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
  368.                    NULL, NULL, NULL, NULL, &type, &crea);
  369.     fd = FileOpen(filecard[selection], name, type, crea,
  370.                   fileModeUpdate | fileModeAnyTypeCreator, NULL);
  371.     WinEraseWindow();
  372.  
  373.     switch (eid) {
  374.     case bunzip:
  375.       archfd = fd;
  376.       n = unzipdir(archfd, filelist, filedisp);
  377.       if (n) {
  378.         maxrow = n;
  379.         FrmGotoForm(unzipform);
  380.       } else {
  381.         FrmAlert(4000);
  382.         FileClose(archfd);
  383.         scandbs();
  384.         FrmGotoForm(decomform);
  385.       }
  386.       return 1;
  387.  
  388.     case buntar:
  389.       archfd = fd;
  390.       n = untardir(archfd, filelist, filedisp);
  391.       if (n) {
  392.         maxrow = n;
  393.         FrmGotoForm(untarform);
  394.       } else {
  395.         FrmAlert(4000);
  396.         FileClose(archfd);
  397.         scandbs();
  398.         FrmGotoForm(decomform);
  399.       }
  400.       return 1;
  401.  
  402.     case bgunzip:
  403.       n = unzip(fd, 0);
  404.       FileClose(fd);
  405.       scandbs();
  406.       FrmGotoForm(decomform);
  407.       return 1;
  408.  
  409.     case bgzip:
  410.       FrmGotoForm(gzipform);
  411.       return 1;
  412.  
  413.     case bripunzip:
  414.  
  415.       WinEraseWindow();
  416.  
  417.       n = unzipdir(fd, filelist, filedisp);
  418.       if( n ) { 
  419.     n = unzip(fd, -1);
  420.  
  421.     FileClose(fd);
  422.     if (n != -1)
  423.       FileDelete(filecard[selection], name);
  424.  
  425.       // Now install the files in filelist.
  426.  
  427.       }
  428.       else {
  429.         WinEraseWindow();
  430.     n = untardir(fd, filelist, filedisp);
  431.     if (n)
  432.       n = untar(fd, -1);
  433.  
  434.     FileClose(fd);
  435.     if (n != -1)
  436.       FileDelete(filecard[selection], name);
  437.  
  438.       // Now install the files in filelist.
  439.  
  440.       }
  441.  
  442.       scandbs();
  443.       FrmGotoForm(decomform);
  444.       return 1;
  445.  
  446.     default:
  447.       return 0;
  448.     }
  449.  
  450.   case tblSelectEvent:
  451.     selection = toprow + event->data.tblSelect.row;
  452.     return 1;
  453.   default:
  454.     return 0;
  455.   }
  456. }
  457.  
  458. /* **************************************************************************** */
  459.  
  460. static Boolean CopyH(EventPtr event)
  461. {
  462.   FormPtr frm;
  463.   FieldPtr textf;
  464.   VoidHand thand;
  465.   VoidPtr tptr;
  466.   UInt32 type, crea, tlen, xlen;
  467.   Word attr;
  468.   FileHand fd, ofd;
  469.   int eid;
  470.   LocalID lid;
  471.   char name[32];
  472.  
  473.   switch (event->eType) {
  474.   case frmOpenEvent:
  475.  
  476.     WinEraseWindow();
  477.     frm = FrmGetActiveForm();
  478.     lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  479.     thand = MemHandleNew(32);
  480.     DmDatabaseInfo(filecard[selection], lid, tptr =
  481.                    MemHandleLock(thand), &attr, NULL, NULL, NULL, NULL, NULL,
  482.                    NULL, NULL, &type, &crea);
  483.  
  484.     fd = FileOpen(filecard[selection], tptr, type, crea,
  485.                   fileModeReadOnly | fileModeAnyTypeCreator, NULL);
  486.  
  487.     FileTell(fd, &tlen, NULL);
  488.     FileClose(fd);
  489.  
  490.     strcat(tptr, "-Cpy");
  491.  
  492.     MemHandleUnlock(thand);
  493.     textf = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, namefld));
  494.     FldSetTextHandle(textf, (Handle) thand);
  495.     FrmSetFocus(frm, FrmGetObjectIndex(frm, namefld));
  496.     FrmDrawForm(frm);
  497.  
  498.     StrIToA(name, tlen);
  499.     strcat(name, " Bytes in box");
  500.     WinDrawChars(name, strlen(name), 5, 45);
  501.     return 1;
  502.  
  503.   case ctlSelectEvent:
  504.  
  505.     eid = event->data.ctlEnter.controlID;
  506.  
  507.     switch (eid) {
  508.     case bok:
  509.  
  510.       // COPY PREVENT?
  511.  
  512.       lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  513.       DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
  514.                      NULL, NULL, NULL, NULL, &type, &crea);
  515.       fd = FileOpen(filecard[selection], name, type, crea,
  516.                     fileModeReadOnly | fileModeAnyTypeCreator, NULL);
  517.       FileTell(fd, &tlen, NULL);
  518.  
  519.       frm = FrmGetActiveForm();
  520.       textf = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, namefld));
  521.       thand = (VoidHand) FldGetTextHandle(textf);
  522.       ofd = FileOpen(filecard[selection], MemHandleLock(thand), 'DATA', 'BOXR',
  523.                      fileModeReadWrite, NULL);
  524.       MemHandleUnlock(thand);
  525.  
  526.       WinEraseWindow();
  527.  
  528.       tptr = MemPtrNew(4096);
  529.  
  530.       crea = tlen;
  531.       WinDrawChars("Copying:", 8, 0, 15);
  532.       while (tlen) {
  533.         RectangleType r = { {0, 30}
  534.         , {0, 15}
  535.         };
  536.  
  537.         type = tlen > 4096 ? 4096 : tlen;
  538.         xlen = FileRead(fd, tptr, 1, type, NULL);
  539.         if (type != xlen)
  540.           break;                // ERROR MESSAGE
  541.         FileWrite(ofd, tptr, 1, xlen, NULL);
  542.         tlen -= xlen;
  543.  
  544.         r.extent.x = 160 - (160 * tlen / crea);
  545.         WinDrawRectangle(&r, 0);
  546.  
  547.       }
  548.       FileClose(fd);
  549.       FileClose(ofd);
  550.       MemPtrFree(tptr);
  551.  
  552.       // FALLTHROUGH
  553.  
  554.     case bexit:
  555.       scandbs();
  556.       FrmGotoForm(fileform);
  557.       return 1;
  558.  
  559.     default:
  560.       return 0;
  561.     }
  562.  
  563.   default:
  564.     return 0;
  565.  
  566.   }
  567. }
  568.  
  569. /* **************************************************************************** */
  570.  
  571. extern void gzip( FileHand fd, FileHand ofd);
  572.  
  573. static Boolean GzipH(EventPtr event)
  574. {
  575.   FormPtr frm;
  576.   FieldPtr textf;
  577.   VoidHand thand;
  578.   VoidPtr tptr;
  579.   UInt32 type, crea, tlen;
  580.   Word attr;
  581.   FileHand fd, ofd;
  582.   int eid, n;
  583.   LocalID lid;
  584.   char name[32];
  585.  
  586.   switch (event->eType) {
  587.   case frmOpenEvent:
  588.  
  589.     WinEraseWindow();
  590.     frm = FrmGetActiveForm();
  591.     lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  592.     thand = MemHandleNew(32);
  593.     DmDatabaseInfo(filecard[selection], lid, tptr =
  594.                    MemHandleLock(thand), &attr, NULL, NULL, NULL, NULL, NULL,
  595.                    NULL, NULL, &type, &crea);
  596.  
  597.     fd = FileOpen(filecard[selection], tptr, type, crea,
  598.                   fileModeReadOnly | fileModeAnyTypeCreator, NULL);
  599.  
  600.     FileTell(fd, &tlen, NULL);
  601.     FileClose(fd);
  602.  
  603.     strcat(tptr, ".gz");
  604.  
  605.     MemHandleUnlock(thand);
  606.     textf = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, namefld));
  607.     FldSetTextHandle(textf, (Handle) thand);
  608.     FrmSetFocus(frm, FrmGetObjectIndex(frm, namefld));
  609.     FrmDrawForm(frm);
  610.  
  611.     StrIToA(name, tlen);
  612.     strcat(name, " Bytes in box");
  613.     WinDrawChars(name, strlen(name), 5, 45);
  614.     return 1;
  615.  
  616.   case ctlSelectEvent:
  617.  
  618.     eid = event->data.ctlEnter.controlID;
  619.  
  620.     switch (eid) {
  621.     case bok:
  622.  
  623.       lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  624.       DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
  625.                      NULL, NULL, NULL, NULL, &type, &crea);
  626.       fd = FileOpen(filecard[selection], name, type, crea,
  627.                     fileModeReadOnly | fileModeAnyTypeCreator, NULL);
  628.       FileTell(fd, &tlen, NULL);
  629.  
  630.       frm = FrmGetActiveForm();
  631.       textf = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, namefld));
  632.       thand = (VoidHand) FldGetTextHandle(textf);
  633.       ofd = FileOpen(filecard[selection], MemHandleLock(thand), 'DATA', 'BOXR',
  634.                      fileModeReadWrite, NULL);
  635.       MemHandleUnlock(thand);
  636.  
  637.       n = FrmAlert(3000);
  638.  
  639.       if( n )
  640.     FileControl(fileOpDestructiveReadMode, fd, NULL, NULL);
  641.  
  642.       WinEraseWindow();
  643.  
  644.       gzip(fd,ofd);
  645.  
  646.       FileClose(fd);
  647.       FileClose(ofd);
  648.  
  649.       if (n)
  650.         FileDelete(filecard[selection], name);
  651.  
  652.       // FALLTHROUGH
  653.  
  654.     case bexit:
  655.       scandbs();
  656.       FrmGotoForm(fileform);
  657.       return 1;
  658.  
  659.     default:
  660.       return 0;
  661.     }
  662.  
  663.   default:
  664.     return 0;
  665.  
  666.   }
  667. }
  668.  
  669. /* **************************************************************************** */
  670.  
  671. static Boolean InfoH(EventPtr event)
  672. {
  673.   FormPtr frm;
  674.   FieldPtr textf;
  675.   VoidHand thand;
  676.   VoidPtr tptr;
  677.   UInt32 type, crea, tlen;
  678.   Word attr;
  679.   FileHand fd;
  680.   int eid;
  681.   LocalID lid;
  682.   char name[32];
  683.  
  684.   switch (event->eType) {
  685.   case frmOpenEvent:
  686.  
  687.     WinEraseWindow();
  688.     frm = FrmGetActiveForm();
  689.     lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  690.     thand = MemHandleNew(32);
  691.     DmDatabaseInfo(filecard[selection], lid, tptr =
  692.                    MemHandleLock(thand), &attr, NULL, NULL, NULL, NULL, NULL,
  693.                    NULL, NULL, &type, &crea);
  694.  
  695.     fd = FileOpen(filecard[selection], tptr, type, crea,
  696.                   fileModeReadOnly | fileModeAnyTypeCreator, NULL);
  697.  
  698.     FileTell(fd, &tlen, NULL);
  699.     FileClose(fd);
  700.     MemHandleUnlock(thand);
  701.     textf = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, namefld));
  702.     FldSetTextHandle(textf, (Handle) thand);
  703.     FrmSetFocus(frm, FrmGetObjectIndex(frm, namefld));
  704.  
  705.     CtlSetValue(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, bbackup)),
  706.                 attr & dmHdrAttrBackup);
  707.     CtlSetValue(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, bcopypr)),
  708.                 attr & dmHdrAttrCopyPrevention);
  709.     CtlSetValue(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, breadon)),
  710.                 attr & dmHdrAttrReadOnly);
  711.  
  712.     FrmDrawForm(frm);
  713.  
  714.     StrIToA(name, tlen);
  715.     strcat(name, " Bytes in box, Type: ");
  716.     name[strlen(name)+4]=0;
  717.     strncat(name, (char *) &crea, 4);
  718.     WinDrawChars(name, strlen(name), 5, 45);
  719.  
  720.     return 1;
  721.  
  722.   case ctlSelectEvent:
  723.  
  724.     eid = event->data.ctlEnter.controlID;
  725.  
  726.     switch (eid) {
  727.  
  728.     case bok:
  729.       frm = FrmGetActiveForm();
  730.       textf = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, namefld));
  731.       thand = (VoidHand) FldGetTextHandle(textf);
  732.  
  733.       lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  734.  
  735.       DmDatabaseInfo(filecard[selection], lid, NULL, &attr, NULL, NULL, NULL,
  736.                      NULL, NULL, NULL, NULL, NULL, NULL);
  737.  
  738.       attr &= ~(dmHdrAttrCopyPrevention | dmHdrAttrBackup | dmHdrAttrReadOnly);
  739.  
  740.       if (CtlGetValue(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, bbackup))))
  741.         attr |= dmHdrAttrBackup;
  742.       if (CtlGetValue(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, bcopypr))))
  743.         attr |= dmHdrAttrCopyPrevention;
  744.       if (CtlGetValue(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, breadon))))
  745.         attr |= dmHdrAttrReadOnly;
  746.  
  747. // will fail if not renamed , or if new name would collide - need to
  748. // display error msg in latter case.
  749.  
  750.       DmSetDatabaseInfo(filecard[selection], lid, MemHandleLock(thand), NULL,
  751.                         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  752.  
  753.       DmSetDatabaseInfo(filecard[selection], lid, NULL, &attr,
  754.                         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  755.  
  756.       MemHandleUnlock(thand);
  757.  
  758.       // Fallthrough
  759.     case bexit:
  760.       scandbs();
  761.       FrmGotoForm(fileform);
  762.       return 1;
  763.  
  764.  
  765.     case bboxr:
  766.       frm = FrmGetActiveForm();
  767.       textf = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, namefld));
  768.       thand = (VoidHand) FldGetTextHandle(textf);
  769.  
  770.       lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  771.       crea = 'BOXR';
  772.       DmSetDatabaseInfo(filecard[selection], lid, NULL, NULL, NULL, NULL, NULL,
  773.                      NULL, NULL, NULL, NULL, NULL, &crea);
  774.  
  775.       MemHandleUnlock(thand);
  776.       FrmGotoForm(infoform);
  777.       return 1;
  778.  
  779.     default:
  780.       return 0;
  781.     }
  782.  
  783.   default:
  784.     return 0;
  785.  
  786.   }
  787. }
  788.  
  789. /* **************************************************************************** */
  790.  
  791. static Boolean FileH(EventPtr event)
  792. {
  793.   Word n;
  794.   LocalID lid;
  795.   FileHand fd;
  796.   UInt32 len, type, crea;
  797.   int eid;
  798.  
  799.   if (basehand(event))
  800.     return 1;
  801.  
  802.   switch (event->eType) {
  803.  
  804.   case ctlSelectEvent:
  805.     eid = event->data.ctlEnter.controlID;
  806.  
  807.     if (eid == bexit) {
  808.       FrmGotoForm(mainform);
  809.       return 1;
  810.     }
  811.  
  812.     if (selection == -1)
  813.       return 1;
  814.  
  815.     switch (eid) {
  816.     case bcpy:
  817.       {
  818.         Word attr;
  819.  
  820.         lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  821.         DmDatabaseInfo(filecard[selection], lid, NULL, &attr, NULL, NULL, NULL,
  822.                        NULL, NULL, NULL, NULL, NULL, NULL);
  823.  
  824.         if (attr & dmHdrAttrCopyPrevention) {
  825.           FrmAlert(2100);
  826.           return 1;
  827.         }
  828.       }
  829.       FrmGotoForm(copyform);
  830.       return 1;
  831.  
  832.     case binfo:
  833.       FrmGotoForm(infoform);
  834.       return 1;
  835.  
  836.     case bdelete:
  837.  
  838.       if (FrmAlert(2000))
  839.         return 1;
  840.  
  841.       lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  842.       n = DmDeleteDatabase(filecard[selection], lid);
  843.  
  844.       scandbs();
  845.       FrmGotoForm(fileform);
  846.       return 1;
  847.  
  848.     case bbeam:
  849.       {
  850.         VoidPtr px;
  851.         Word err;
  852.         long unsigned int xcnt, outhis, tlen;
  853.         ExgSocketType exgSocket;
  854.         char name[36], buf[256];
  855.         Word attr;
  856.  
  857.         lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  858.         DmDatabaseInfo(filecard[selection], lid, name, &attr, NULL, NULL, NULL,
  859.                        NULL, NULL, NULL, NULL, &type, &crea);
  860.  
  861.         if (attr & dmHdrAttrCopyPrevention) {
  862.           FrmAlert(2100);
  863.           return 1;
  864.         }
  865.  
  866.         WinDrawChars(name, strlen(name), 80, 0);
  867.  
  868.         fd = FileOpen(filecard[selection], name, type, crea,
  869.                       fileModeReadOnly | fileModeAnyTypeCreator, NULL);
  870.         FileTell(fd, &len, NULL);
  871.  
  872.         MemSet(&exgSocket, sizeof(exgSocket), 0);
  873.         exgSocket.description = name;
  874.         exgSocket.name = name;
  875.         exgSocket.length = len;
  876.         exgSocket.target = 'BOXR';
  877.  
  878.         err = ExgPut(&exgSocket);
  879.  
  880.         if (!err) {
  881.           xcnt = 0;
  882.           while (xcnt < len) {
  883.  
  884.             tlen = FileRead(fd, buf, 1, 256, NULL);
  885.             xcnt += tlen;
  886.             px = buf;
  887.             while (tlen) {
  888.               outhis = ExgSend(&exgSocket, px, tlen, &err);
  889.               if (err)
  890.                 break;
  891.               tlen -= outhis;
  892.               px += outhis;
  893.             }
  894.  
  895.           }
  896.           FileClose(fd);
  897.           EvtResetAutoOffTimer();
  898.  
  899.           ExgDisconnect(&exgSocket, err);
  900.         }
  901.     default:
  902.         return 0;
  903.       }
  904.  
  905.     }
  906.  
  907.   case tblSelectEvent:
  908.     selection = toprow + event->data.tblSelect.row;
  909.     return 1;
  910.   default:
  911.     return 0;
  912.   }
  913. }
  914.  
  915. /* **************************************************************************** */
  916.  
  917. static Boolean InstH(EventPtr event)
  918. {
  919.   Word n;
  920.   LocalID lid;
  921.   FileHand fd;
  922.   char name[256];
  923.   UInt32 type, crea;
  924.   int eid;
  925.  
  926.   if (basehand(event))
  927.     return 1;
  928.  
  929.   switch (event->eType) {
  930.  
  931.   case ctlSelectEvent:
  932.  
  933.     eid = event->data.ctlEnter.controlID;
  934.  
  935.     if (eid == bexit) {
  936.       FrmGotoForm(mainform);
  937.       return 1;
  938.     }
  939.  
  940.     if (selection == -1)
  941.       return 1;
  942.  
  943.     switch (eid) {
  944.  
  945.     case btodoc:
  946.  
  947.       lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  948.       DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
  949.                      NULL, NULL, NULL, NULL, &type, &crea);
  950.       fd = FileOpen(filecard[selection], name, type, crea,
  951.                     fileModeUpdate | fileModeAnyTypeCreator, NULL);
  952.  
  953.       strcat(name, ".box");
  954.  
  955.       n = FrmAlert(3000);
  956.  
  957.       WinEraseWindow();
  958.  
  959.       MakeDoc(name, fd, n);
  960.       FileClose(fd);
  961.       if (n) {
  962.         name[strlen(name) - 4] = 0;
  963.         FileDelete(filecard[selection], name);
  964.       }
  965.       scandbs();
  966.       FrmGotoForm(instform);
  967.  
  968.       return 1;
  969.  
  970.     case binstall:
  971.  
  972.       lid = DmGetDatabase(filecard[selection], filedisp[selection]);
  973.       DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
  974.                      NULL, NULL, NULL, NULL, &type, &crea);
  975.       fd = FileOpen(filecard[selection], name, type, crea,
  976.                     fileModeUpdate | fileModeAnyTypeCreator, NULL);
  977.  
  978.       n = FrmAlert(3000);
  979.       WinEraseWindow();
  980.       MakeDatabase(fd, n);
  981.       FileClose(fd);
  982.       if (n)
  983.         FileDelete(filecard[selection], name);
  984.  
  985.       scandbs();
  986.       FrmGotoForm(instform);
  987.  
  988.       return 1;
  989.     default:
  990.       return 0;
  991.     }
  992.  
  993.   case tblSelectEvent:
  994.     selection = toprow + event->data.tblSelect.row;
  995.     return 1;
  996.   default:
  997.     return 0;
  998.   }
  999.  
  1000. }
  1001.  
  1002. /* **************************************************************************** */
  1003.  
  1004. int dirtyp;
  1005.  
  1006. DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
  1007. {
  1008.   short err;
  1009.   int formID;
  1010.   FormPtr form = NULL;
  1011.   EventType event;
  1012.   LocalID lid;
  1013.   DmOpenRef scratch;
  1014.   Word dbrec;
  1015.   VoidHand hand;
  1016.  
  1017.   if (cmd == sysAppLaunchCmdExgAskUser) {
  1018.     ExgAskParamPtr exgP = (ExgAskParamPtr) cmdPBP;
  1019.     exgP->result = exgAskDialog; /* Dialog, Ok, Cancel */
  1020.     return 0;
  1021.   } else if (cmd == sysAppLaunchCmdExgReceiveData) {
  1022.     ExgSocketPtr exgSocketP = (ExgSocketPtr) cmdPBP;
  1023.     unsigned char buf[256];
  1024.     int inthis;
  1025.     FileHand fd;
  1026.  
  1027.     err = ExgAccept(exgSocketP);
  1028.     fd = FileOpen(0, exgSocketP->name, 'DATA', 'BOXR', fileModeReadWrite, NULL);
  1029.  
  1030.     if ((launchFlags & sysAppLaunchFlagSubCall)) {
  1031.       CALLBACK_PROLOGUE;
  1032.       dirtyp++;
  1033.       CALLBACK_EPILOGUE;
  1034.     }
  1035.  
  1036.     if (err)
  1037.       return err;
  1038.  
  1039.     inthis = 1;
  1040.     while (inthis) {
  1041.       inthis = ExgReceive(exgSocketP, buf, 256, &err);
  1042.       if (err)
  1043.         break;
  1044.       FileWrite(fd, buf, 1, inthis, NULL);
  1045.     }
  1046.     FileClose(fd);
  1047.     ExgDisconnect(exgSocketP, err);
  1048.     EvtResetAutoOffTimer();
  1049.     return 0;
  1050.   } else if (cmd != sysAppLaunchCmdNormalLaunch)
  1051.     return 0;
  1052.  
  1053.   toprow = 0;
  1054.   dirtyp = 1;
  1055.  
  1056.   //SCRATCHPAD
  1057.  
  1058.   lid = DmFindDatabase(0, "BOXERSCRATCHPAD");
  1059.   if (lid)
  1060.     DmDeleteDatabase(0, lid);
  1061.   DmCreateDatabase(0, "BOXERSCRATCHPAD", 'BOXR', 'DATA', false);
  1062.   lid = DmFindDatabase(0, "BOXERSCRATCHPAD");
  1063.   scratch = DmOpenDatabase(0, lid, dmModeReadWrite);
  1064.   if (!scratch)
  1065.     ErrDisplay("Could not create scratchpad");
  1066.   dbrec = 0;
  1067.   hand = DmNewRecord(scratch, &dbrec, 8192);
  1068.   filelist = MemHandleLock(hand);
  1069.   hand = DmNewRecord(scratch, &dbrec, 4096);
  1070.   filecard = MemHandleLock(hand);
  1071.   hand = DmNewRecord(scratch, &dbrec, 2048);
  1072.   filedisp = MemHandleLock(hand);
  1073.  
  1074.   do {
  1075.     if (dirtyp) {
  1076.       scandbs();
  1077.       FrmGotoForm(mainform);
  1078.       dirtyp = 0;
  1079.     }
  1080.  
  1081.     EvtGetEvent(&event, -1);
  1082.  
  1083.     if (selection < toprow || selection > maxrow || selection > toprow + 10)
  1084.       selection = -1;
  1085.  
  1086.     if (SysHandleEvent(&event))
  1087.       continue;
  1088.     if (MenuHandleEvent((void *) 0, &event, &err))
  1089.       continue;
  1090.     if (event.eType == frmLoadEvent) {
  1091.       formID = event.data.frmLoad.formID;
  1092.       form = FrmInitForm(formID);
  1093.       FrmSetActiveForm(form);
  1094.       switch (formID) {
  1095.  
  1096.       default:
  1097.  
  1098.       case mainform:
  1099.         FrmSetEventHandler(form, MainH);
  1100.         break;
  1101.       case fileform:
  1102.         FrmSetEventHandler(form, FileH);
  1103.         break;
  1104.       case decomform:
  1105.         FrmSetEventHandler(form, DecomH);
  1106.         break;
  1107.       case instform:
  1108.         FrmSetEventHandler(form, InstH);
  1109.         break;
  1110.       case unzipform:
  1111.         FrmSetEventHandler(form, UnzipH);
  1112.         break;
  1113.       case untarform:
  1114.         FrmSetEventHandler(form, UntarH);
  1115.         break;
  1116.       case copyform:
  1117.         FrmSetEventHandler(form, CopyH);
  1118.         break;
  1119.       case gzipform:
  1120.         FrmSetEventHandler(form, GzipH);
  1121.         break;
  1122.       case infoform:
  1123.         FrmSetEventHandler(form, InfoH);
  1124.         break;
  1125.       }
  1126.  
  1127.     }
  1128.     if (form)
  1129.       FrmDispatchEvent(&event);
  1130.   } while (event.eType != appStopEvent);
  1131.  
  1132.   MemPtrUnlock(filedisp);
  1133.   MemPtrUnlock(filecard);
  1134.   MemPtrUnlock(filelist);
  1135.   DmCloseDatabase(scratch);
  1136.   DmDeleteDatabase(0, lid);
  1137.   return 0;
  1138. }
  1139.